package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.app.mapper.RoomInfoMapper;
import com.atguigu.lease.web.app.service.*;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.attr.AttrValueVo;
import com.atguigu.lease.web.app.vo.fee.FeeValueVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.atguigu.lease.web.app.vo.room.RoomDetailVo;
import com.atguigu.lease.web.app.vo.room.RoomItemVo;
import com.atguigu.lease.web.app.vo.room.RoomQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
@Slf4j
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    private ApartmentInfoService apartmentInfoService;
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private AttrValueService attrValueService;
    @Autowired
    private FacilityInfoService facilityInfoService;
    @Autowired
    private LabelInfoService labelInfoService;
    @Autowired
    private PaymentTypeService paymentTypeService;
    @Autowired
    private FeeValueService feeValueService;
    @Autowired
    private LeaseTermService leaseTermService;
    @Autowired
    private LeaseAgreementService leaseAgreementService;

    /**
     * 分页查询房间列表
     *
     * @param current 当前页
     * @param size    每页大小
     * @param queryVo 查询条件
     * @return 当前页房间的信息
     */
    @Override
    public IPage<RoomItemVo> getRoomItemVoListByQuery(long current, long size, RoomQueryVo queryVo) {
        return baseMapper.selectRoomItemVoListByQuery(new Page<>(current, size), queryVo);
    }

    /**
     * 根据id获取房间的详细信息
     *
     * @param id 房间id
     * @return 房间的详细信息
     */
    @Override
    public RoomDetailVo getRoomDataVoById(Long id) {

        // 获取房间的基本信息
        RoomInfo roomInfo = getById(id);
        if (roomInfo == null) {
            return null;
        }

        // 获取所属公寓详细信息
        ApartmentItemVo apartmentItemVo = apartmentInfoService.getApartmentItemVoById(roomInfo.getApartmentId());
        // 获取房间的图片信息列表
        List<GraphVo> graphVoList = graphInfoService.getGraphVoListByRoomId(roomInfo.getId());
        // 获取房间的属性信息列表
        List<AttrValueVo> attrValueList = attrValueService.getAttrValueListByRoomId(roomInfo.getId());
        // 获取房间的配套信息列表
        List<FacilityInfo> facilityInfoList = facilityInfoService.getFacilityInfoListByRoomId(roomInfo.getId());
        // 获取房间的标签信息列表
        List<LabelInfo> labelInfoList = labelInfoService.getLabelListByRoomId(roomInfo.getId());
        // 获取支付方式列表
        List<PaymentType> paymentTypeListById = paymentTypeService.getPaymentTypeListById(roomInfo.getId());
        // 获取房间的杂费信息列表
        List<FeeValueVo> feeValueVoList = feeValueService.getFeeValueListById(roomInfo.getApartmentId());
        // 获取房间的可选租期列表
        List<LeaseTerm> leaseTermList = leaseTermService.selectTermListByRoomId(roomInfo.getId());
        // 判断房间是否被删除
        boolean isDeleted = roomInfo.getIsDeleted() == 1;
        // 判断房间是否已入住
        LambdaQueryWrapper<LeaseAgreement> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LeaseAgreement::getRoomId, roomInfo.getId());
        queryWrapper.in(LeaseAgreement::getStatus, LeaseStatus.SIGNED, LeaseStatus.WITHDRAWING);
        boolean isCheckIn = false;
        if (leaseAgreementService.count(queryWrapper) > 0) {
            isCheckIn = true;
        }

        // 创建房间的Vo对象
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        // 装配房间基本信息
        BeanUtils.copyProperties(roomInfo, roomDetailVo);
        // 装配房间的所属公寓信息
        roomDetailVo.setApartmentItemVo(apartmentItemVo);
        // 装配房间的图片信息
        roomDetailVo.setGraphVoList(graphVoList);
        // 装配房间的属性列表
        roomDetailVo.setAttrValueVoList(attrValueList);
        // 装配房间的配套列表
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        // 装配房间的标签列表
        roomDetailVo.setLabelInfoList(labelInfoList);
        // 装配房间的支付方式列表
        roomDetailVo.setPaymentTypeList(paymentTypeListById);
        // 装配房间的杂费信息
        roomDetailVo.setFeeValueVoList(feeValueVoList);
        // 装配房间的可选租期列表
        roomDetailVo.setLeaseTermList(leaseTermList);
        // 装配房间是否被删除
        roomDetailVo.setIsDelete(isDeleted);
        // 装配房间是否已入住
        roomDetailVo.setIsCheckIn(isCheckIn);

        return roomDetailVo;
    }

    /**
     * 公寓房间最低租金
     *
     * @param id 公寓id
     * @return 最低租金
     */
    @Override
    public BigDecimal getMinRentByApartmentId(Long id) {
        return baseMapper.selectMinRentByApartmentId(id);
    }

    /**
     * 根据公寓id分页查询房间列表
     *
     * @param current 当前页
     * @param size    每页大小
     * @param id      公寓id
     * @return 当前页房间的信息
     */
    @Override
    public IPage<RoomItemVo> getRoomItemVoListByQueryAndApartmentId(long current, long size, Long id) {
        return baseMapper.selectRoomItemVoListByQueryAndApartmentId(new Page<>(current, size), id);
    }
}




